<?php

declare(strict_types=1);

namespace App\Services;

use App\BO\HomepageBO;
use App\Entity\HomepageComboEntity;
use App\Entity\HomepageRecommendEntity;
use App\Models\HomepageCombo;
use App\Models\HomepageRecommend;

class HomepageService
{
    /**
     * 获取城市推荐产品列表
     * @param int $cityId
     * @param bool $withoutHidden
     * @param string $sortBy
     * @return array
     */
    public function getCityRecommendList($cityId = 0, $withoutHidden = true, $sortBy = '')
    {
        $cityRecommendObjs = HomepageRecommend::getCityRecommend($cityId, $withoutHidden, $sortBy);
        $data = [];
        if ($cityRecommendObjs) {
            $cityUniques = []; //城市独有配置
            foreach ($cityRecommendObjs as $recommendObj) {
                if ($recommendObj->city_id > 0) {
                    $cityUniques[] = $recommendObj->product_id;
                }
            }
            foreach ($cityRecommendObjs as $index => $recommendObj) {
                if (!$recommendObj->city_id && in_array($recommendObj->product_id, $cityUniques)) {
                    unset($cityRecommendObjs[$index]);
                    continue;
                }
                $data[] = new HomepageRecommendEntity($recommendObj);
            }
        }

        return $data;
    }

    /**
     * 城市套餐列表
     * @param int $cityId
     * @param bool $withHidden
     * @param string $sortBy
     * @return array
     */
    public function getCityComboList($cityId = 0, $withHidden = false, $sortBy = '')
    {
        $cityComboObjs = HomepageCombo::getCityCombo($cityId, $withHidden, $sortBy);
        $data = [];
        if ($cityComboObjs) {
            foreach ($cityComboObjs as $comboObj) {
                $data[] = $comboObj->toArray();
            }
        }

        return $data;
    }

    public function getCombo($id)
    {
        return HomepageCombo::find($id);
    }

    /**
     * 保存套餐
     * @param array $combo
     * @param int $id
     * @return bool
     */
    public function saveCombo(array $combo, int $id = 0)
    {
        if ($id) {
            $comboObj = HomepageCombo::find($id);
        } else {
            $comboObj = new HomepageCombo();
        }
        $comboEntity = new HomepageComboEntity($comboObj);
        for ($i = 1; $i <= 5; $i++) {
            if (isset($combo['text' . $i])) {
                $comboEntity->setText($i, $combo['text' . $i]);
            }
        }
        for ($i = 1; $i <= 5; $i++) {
            if (isset($combo['image' . $i])) {
                $comboEntity->setImage($i, $combo['image' . $i]);
            }
        }
        if (isset($combo['link_url'])) {
            $comboEntity->setLinkUrl($combo['link_url']);
        }
        if (isset($combo['origin_price'])) {
            $comboEntity->setOriginPrice($combo['origin_price']);
        }
        if (isset($combo['lower_price'])) {
            $comboEntity->setLowerPrice($combo['lower_price']);
        }
        if (isset($combo['sort_weight'])) {
            $comboEntity->setSortWeight($combo['sort_weight']);
        }
        if (isset($combo['start_datetime'])) {
            $comboEntity->setStartDatetime($combo['start_datetime']);
        }
        if (isset($combo['end_datetime'])) {
            $comboEntity->setEndDatetime($combo['end_datetime']);
        }
        if (isset($combo['hidden'])) {
            $comboEntity->setHidden($combo['hidden']);
        }
        if (isset($combo['city_id'])) {
            $comboEntity->setCityId(intval($combo['city_id']));
        }

        return $comboEntity->save();
    }

    /**
     * 删除套餐
     * @param $id
     * @return mixed
     */
    public function removeCombo($id)
    {
        $comboObj = HomepageCombo::find($id);

        return $comboObj->delete();
    }

    //############

    public function saveRecommend(array $recommend, int $id = 0)
    {
        if ($id) {
            $recommendObj = HomepageRecommend::find($id);
        } else {
            $recommendObj = new HomepageRecommend();
        }
        $recommendEntity = new HomepageRecommendEntity($recommendObj);
        $recommendEntity->modelSetData($recommend);

        return $recommendEntity->save();
    }

    public function setRecommendHidden(int $id)
    {
        return $this->saveRecommend(['hidden' => 1], $id);
    }

    public function getRecommend(int $id)
    {
        return HomepageRecommend::find($id);
    }

    public function addRecommend(array $recommend)
    {
        return $this->saveRecommend($recommend);
    }

    public function removeRecommend($id)
    {
        return HomepageRecommend::where(['id' => $id])->delete();
    }

    public function getHomepageExtraValues()
    {
        $extraValues = [];
        $convertibleCardInfo = HomepageBO::EXTRA_VALUES_CONVERTIBLECARD_INFO;
        $extraValues = array_merge([$convertibleCardInfo], HomepageBO::EXTRA_VALUES_PRODUCT);
        return $extraValues;
    }
}
